const Service = require('egg').Service;

class AccessPlatformService extends Service {

  // 获取账户信息
  async getAccountDetail (id) {
    const { app } = this;
    try {
      if (id) {
        return await app.model.Account.findOne({
          where: { id },
          include: [{
            model: app.model.Role,
            as: 'role',//这里的 as需要与之前定义的as名字相同
          }]
        });
      }
      return await app.model.Account.findAll({
        include: [{
          model: app.model.User,
          as: 'user',//这里的 as需要与之前定义的as名字相同
        }]
      });
    } catch (err) {
      console.log(err);
      return null;
    }
  }

  // 获取用户列表
  async getUserdetail () {
    const { app } = this
    try {
      return await app.model.User.findAll({
        include: {
          model: app.model.Role,
        },
      })
    } catch (err) {
      console.log(err)
      return null
    }
  }

  // 获取角色权限
  async getRoledetail () {
    const { app } = this
    try {
      return await app.model.Role.findAll({
        include: {
          model: app.model.Permission,
        },
      })
    } catch (err) {
      console.log(err)
      return null
    }
  }


}

module.exports = AccessPlatformService;